package com.topgay.dao;

import java.util.HashMap;
import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.topgay.domain.CSCenter;
import com.topgay.domain.Declaration;
import com.topgay.util.BoardUtilities;

@Repository
public class CSCenterDAOImpl implements CSCenterDAO{

	@Autowired
	private SqlSessionTemplate session;
	
	@Override
	public List<CSCenter> selectCSCenterList(int page, int divide) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("divide", divide);
		return session.selectList(makeSqlId("selectCSCenterList"), map);
	}
	
	@Override
	public int selectTotalContent(int divide){
		return session.selectOne(makeSqlId("selectTotalContent"), divide);
	}

	@Override
	public int insertCSCenterContent(CSCenter cscenter, int divide) {
		return session.insert(makeSqlId("insertCSCenterContent"), cscenter);
	}
	

	@Override
	public int updateCSCenterViewCount(int cscenterNo) {
		return session.update(makeSqlId("updateCSCenterViewCount"), cscenterNo);
	}
	
	@Override
	public int updateCSCenterContent(CSCenter cscenter) {
		return session.update(makeSqlId("updateCSCenterContent"), cscenter);
	}

	@Override
	public CSCenter selectCSCenterContentByNo(int cscenterNo) {
		return session.selectOne(makeSqlId("selectCSCenterContentByNo"), cscenterNo);
	}
	
	@Override
	public int deleteContentByNO(int cscenterNo) {
		return session.delete(makeSqlId("deleteContentByNo"), cscenterNo);
	}


	@Override
	public int selectDeclarationTotalContent() {
		return session.selectOne(makeSqlIdForDeclaration("selectDeclarationTotalContent"));
	}
	
	@Override
	public int insertDeclarationContent(Declaration declaration) {
		return session.insert(makeSqlIdForDeclaration("insertDeclarationContent"), declaration);
	}

	@Override
	public List<Declaration> selectDeclarationList(int page) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		return session.selectList(makeSqlIdForDeclaration("selectDeclarationList"), map);
	}

	@Override
	public List<Declaration> selectDeclarationListByAssaulterId(int page, String assaulterId) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("assaulterId", assaulterId);
		return session.selectList(makeSqlIdForDeclaration("selectDeclarationListByAssaulterId"), map);
	}

	@Override
	public List<Declaration> selectDeclarationListByAssaulterNickname(int page, String assaulterNickname) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("assaulterNickname", assaulterNickname);
		return session.selectList(makeSqlIdForDeclaration("selectDeclarationListByAssaulterId"), map);
	}
	
	
	@Override
	public List<Declaration> selectDeclarationListById(int page, String id) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("id", id);
		return session.selectList(makeSqlIdForDeclaration("selectDeclarationListById"), map);
	}

	@Override
	public Declaration selectDeclarationContentByNo(int declarationNo) {
		return session.selectOne(makeSqlIdForDeclaration("selectDeclarationContentByNo"), declarationNo);
	}

	@Override
	public int updateDeclarationForResult(int declarationNo, String result) {
		HashMap map = new HashMap();
		map.put("declarationNo", declarationNo);
		map.put("result", result);
		return session.update(makeSqlIdForDeclaration("updateDeclarationForResult"), map);
	}

	@Override
	public int deleteDeclarationContentByNo(int declarationNo) {
		return session.delete(makeSqlIdForDeclaration("deleteDeclarationContentByNo"), declarationNo);
	}

	private String makeSqlId(String id){
		return "cscenter." + id;
	}

	private String makeSqlIdForDeclaration(String id){
		return "declaration." + id;
	}
}
